<template>
  <div>
    <el-table
      v-if="tableData"
      :data="
        tableData.filter(
          (data) =>
            !search || data.aname.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column type="index" prop="id" width="50"> </el-table-column>
      <el-table-column
        prop="aname"
        label="管理员名"
        width="180"
        sortable
      ></el-table-column>
      <!-- <el-table-column
        prop="apwd"
        label="管理员密码"
        width="180"
      ></el-table-column> -->

      <el-table-column align="right">
        <template slot="header" slot-scope="{}">
          <el-input v-model="search" size="mini" placeholder="输入管理员名搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除吗？"
            @onConfirm="handleDelete(scope.$index, scope.row)"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-row>
      <el-col :span="20" :offset="4">
        <el-pagination
          layout="prev, pager, next, jumper, total, sizes"
          background
          prev-text="prev"
          next-text="next"
          :page-size="size"
          :current-page="pageNow"
          :page-sizes="[5, 10, 15, 20]"
          @current-change="findPage"
          @size-change="findSize"
          :total="total"
        >
        </el-pagination>
      </el-col>
    </el-row>

    <el-button
      type="success"
      @click="saveAdminInfo"
      size="mini"
      style="margin-top: 10px"
      >添加</el-button
    >

    <!-- 添加管理员按钮 -->
    <div>
      <div style="display: flex; margin-top: 20px">
        <transition name="el-zoom-in-center">
          <div v-show="show" class="transition-box">
            <el-form
              ref="form"
              label-suffix=":"
              :model="form"
              label-width="100px"
            >
              <el-form-item label="管理员姓名">
                <el-input v-model="form.aname"></el-input>
              </el-form-item>

              <el-form-item label="管理员密码">
                <el-input v-model="form.apwd"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <el-button @click="saveAdminInfo">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
import { queryAdminList } from "@/network/admin";
import { addOrUpdate } from "@/network/admin";
import { deleteAdmin } from "@/network/admin";
export default {
  name: "admin",
  data() {
    return {
      tableData: [{}],
      form: {},
      search: "",
      show: false,
      total: 0,
      size: 5,
      pageNow: 0,
    };
  },
  created() {
    queryAdminList().then((res) => {
      console.log(res);
      console.log(res.admins);
      this.tableData = res.admins;
      this.total = res.total;
    });
  },
  methods: {
    // 处理
    findSize(size) {
      // console.log(size);
      this.size = size;
      this.findAllTableData((this.page - 1) * size, size);
    },
    // 处理分页的方法
    findPage(page) {
      console.log("page",page);
      this.pageNow = page;
      console.log((page - 1) * this.size);
      this.findAllTableData((page - 1) * this.size, this.size);
    },
    saveAdminInfo() {
      this.show = true;
    },
    handleEdit(index, row) {
      // console.log(index, row);
      this.show = true; // 展示编辑表单
      this.form = row; // 回显
    },
    handleDelete(index, row) {
      console.log(row.aid);
      deleteAdmin(row.aid).then((res) => {
        if (res.status) {
          this.$message({
            message: res.msg,
            type: "success",
          });
          // 刷新数据
          console.log(this.pageNow, this.size);
          this.findAllTableData(this.pageNow, this.size);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    onSubmit() {
      addOrUpdate(this.form).then((res) => {
        if (res.status) {
          this.$message({
            message: "恭喜你，" + res.msg,
            type: "success",
          });
          // 清空表单信息
          this.form = {}
          // 隐藏表单
          this.show = false;
          // 刷新数据
          queryAdminList().then((res) => {
            console.log(res);
            console.log(res.admins);
            this.tableData = res.admins;
            this.total = res.total;
            console.log("Admin", this.Admin);
          });
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    findAllTableData(page, size) {
      console.log("findAllTableData", page, size);
      page = page ? page : this.pageNow;
      size = size ? size : this.size;
      // 记住传参
      queryAdminList(page, size).then((res) => {
        console.log(res.admins);
        this.tableData = res.admins;
        this.total = res.total;
      });
    },
  },
};
</script>

<style>
.transition-box {
  margin-bottom: 10px;
  width: 100%;
  height: 500px;
  border-radius: 4px;
  padding: 40px 20px;
  box-sizing: border-box;
  margin-right: 20px;
}
</style>
